package cn.kgc.dm.service.impl;

import cn.kgc.dm.client.RestDmImageClient;
import cn.kgc.dm.client.RestDmUserClient;
import cn.kgc.dm.common.Constants;
import cn.kgc.dm.common.EmptyUtils;
import cn.kgc.dm.common.RedisUtils;
import cn.kgc.dm.pojo.DmUser;
import cn.kgc.dm.service.LoginService;
import cn.kgc.dm.service.TokenService;
import cn.kgc.dm.vo.DmUserVO;
import cn.kgc.dm.vo.TokenVO;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author: 12138
 * @CreateTime: 2019/05/14
 */
@Service
public class LoginServiceImpl implements LoginService {
	
	@Autowired
	private RestDmUserClient restDmUserClient;
	@Autowired
	private TokenService tokenService;
	@Autowired
	private RedisUtils redisUtils;
	@Autowired
	private RestDmImageClient restDmImageClient;
	
	@Override
	public ArrayList login(String phone, String password, String agent) throws Exception {
		ArrayList list = new ArrayList();
		Map<String, Object> map = new HashMap<>();
		map.put("phone", phone);
		List<DmUser> dmUserListByMap = restDmUserClient.getDmUserListByMap(map);
		if(EmptyUtils.isEmpty(dmUserListByMap)){
			throw new Exception("用户名不存在");
		}
		DmUser dmUser = dmUserListByMap.get(0);
		if(!password.equals("123456")){
			throw new Exception("密码错误");
		}
		String uuid = tokenService.generateUuid(phone);
		String token = (String) redisUtils.get(uuid);
		DmUserVO dmUserVO = new DmUserVO();
		if(EmptyUtils.isNotEmpty(token)){
			dmUserVO = tokenService.load(token);
			redisUtils.delete(token);
			token = tokenService.generateToken(agent, dmUserVO);
		}
		if(EmptyUtils.isEmpty(token)){
			BeanUtils.copyProperties(dmUser, dmUserVO);
			dmUserVO.setUserId(dmUser.getId());
			token = tokenService.generateToken(agent, dmUserVO);
			String imgUrl = getImageUrl(restDmImageClient, dmUser.getId(), Constants.Image.ImageType.normal, Constants.Image.ImageCategory.user);
			dmUserVO.setImgUrl(imgUrl);
		}
		tokenService.saveUuid(uuid, token);
		tokenService.saveToken(token, dmUserVO);
		list.add(dmUserVO);
		TokenVO tokenVO = new TokenVO();
		tokenVO.setToken(token);
		tokenVO.setExpTime(redisUtils.getExpire(token));
		tokenVO.setGenTime(Calendar.getInstance().getTimeInMillis());
		list.add(tokenVO);
		return list;
	}
	
	
	
	public String getImageUrl(RestDmImageClient restDmImageClient,
	                          Long id, Integer type,
	                          Integer category) throws Exception {
		
		String imageToken = Constants.IMAGE_TOKEN_PREFIX + id + "_" + type + "_" + category;
		String imageUrl = (String) redisUtils.get(imageToken);
		if (EmptyUtils.isEmpty(imageUrl)) {
			imageUrl = EmptyUtils.isEmpty
					(restDmImageClient.queryDmImageList(id, type, category))
					?
					Constants.DEFAULT_NORMAL
					:
					restDmImageClient.queryDmImageList(id, type, category)
							.get(0).getImgUrl();
			redisUtils.set(imageToken, imageUrl);
		}
		return imageUrl;
	}
}


